package com.bite.book.service;

import com.bite.book.enums.BookStatus;
import com.bite.book.mapper.BookMapper;
import com.bite.book.model.BookInfo;
import com.bite.book.model.PageRequest;
import com.bite.book.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:DELL
 * Date:2025-05-09
 * Time:10:52
 */
//@Component//spring可以帮我们创建BookService对象
@Service
public class BookService {//对数据进行加工处理

    @Autowired//调用Mapper
    private BookMapper bookMapper;
//查询图书
    public Integer insertBook(BookInfo bookInfo) {//添加图书
        return bookMapper.insertBook(bookInfo);
    }
//获取图书列表
    public PageResult<BookInfo> getBookListByPage(PageRequest pageRequest) {//调用mapper，分页
        //1.获取总记录数
        Integer count=bookMapper.count();
        // 2.获取当前页的记录
        List<BookInfo> bookInfos = bookMapper.queryBookByPage(pageRequest.getOffset(), pageRequest.getPageSize());
        //3.处理状态
        //4. 状态 0-删除, 1- 可借阅   2- 不可借阅
        for(BookInfo bookInfo:bookInfos){//通过枚举获取
            bookInfo.setStatusCN(BookStatus.getDescByCode(bookInfo.getStatus()).getDesc());
        }
        return new PageResult(bookInfos,count,pageRequest);
    }
//根据ID获取图书信息
    public BookInfo queryBookById(Integer bookId) {

        BookInfo bookInfo= bookMapper.queryBookById(bookId);//调用mapper
        bookInfo.setStatusCN(BookStatus.getDescByCode(bookInfo.getStatus()).getDesc());
        return bookInfo;
    }
//更新图书
    public Integer updateBookById(BookInfo bookInfo) {
        return bookMapper.updateBookById(bookInfo);
    }

    //删除图书
    public Integer deleteBook(Integer bookId) {
        BookInfo bookInfo=new BookInfo();
        bookInfo.setId(bookId);
        bookInfo.setStatus(0);
        return bookMapper.updateBookById(bookInfo);
    }
//批量删除图书
    public Integer batchDeleteBookByIds(List<Integer> ids) {
        return bookMapper.batchDeleteBookByIds(ids);
    }
}
